package com.ly.ssh.controller;

import com.ly.ssh.Util.Page;
import com.ly.ssh.entity.Address;
import com.ly.ssh.entity.Person;
import com.ly.ssh.service.AddressService;
import com.ly.ssh.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by XRom
 * On 11/16/2017.11:59 PM
 */
@Controller
public class TestController {

    @Autowired(required=true)
    private PersonService personService;
    @Autowired(required=true)
    private AddressService addressService;
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        return "test";
    }

    @RequestMapping(value = "/savePerson")
    @ResponseBody
    public String savePerson() {
        addressService.saveAddress();
        return "success!";
    }
    @RequestMapping(value = "/getAll")
    public String getAll(ModelMap map) {
        List<Person> personList = personService.getAll();
        map.addAttribute("list",personList);
        return "test";
    }
    @RequestMapping(value = "/getAllAddress")
    public String getAllAddress(ModelMap map) {
        List<Address> addressList = addressService.getAll();
        map.addAttribute("list",addressList);
        return "test1";
    }
    @RequestMapping(value = "/showPage")
    public String findAllCourse(HttpServletRequest request,
                                HttpServletResponse response) {
        try {
            String pageNo = request.getParameter("pageNo");
            if (pageNo == null) {
                pageNo = "1";
            }
            Page page = personService.queryForPage(Integer.valueOf(pageNo), 10);
            request.setAttribute("page", page);
            List<Person> personList =(List<Person>) page.getList();
            request.setAttribute("list", personList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "page";
    }
}
